package Java.view.game.dian21;

import Java.view.game.doudizhurule.MyThread;
import Java.view.game.doudizhurule.Server;
import domain.Player;
import domain.User;

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.*;


public class Room21 {
    ArrayList<Socket> array;
    static int score1;
    static int score2;
    static int count=0;
    static int o=0;
    public static int buyao=0;
    static ArrayList<Player> playerArray;
    public static User[] users=new User[2];

    //收到牌的次数
    static public int times=0;

    int start;
    static private BufferedWriter dos1;
    static private BufferedWriter dos2;

    static ArrayList<String> player1Card = new ArrayList<>();

    static ArrayList<String> player2Card = new ArrayList<>();
    static ArrayList<String> list = new ArrayList<>();//代表一副牌
    static HashMap<String, Integer> hm = new HashMap<>();//将J~王映射成数字

    static {
        //准备牌
        //运用静态代码块，随着类的加载而加载，而且只执行一次
        //牌库
        String[] color = {"♦", "♣", "♥", "♠"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        for (int i = 0; i < color.length; i++) {
            //i依此表示每一种花色
            for (int j = 0; j < number.length; j++) {
                //j表示每一个数字
                list.add(color[i] + number[j]);//每一张牌由花色和数字组成，一共4*13=52张牌
            }
        }
        //指定牌的价值
        //牌上的数字到Map集合中判断是否存在
        //存在，获取价值
        //不存在，本身的数字就是价值
        hm.put("J", 10);
        hm.put("Q", 10);
        hm.put("K", 10);
        hm.put("A", 1);

    }

    public Room21(){}

    //创建房间时，将两个玩家加进来
    public void addPlayer(ArrayList<Player> players){
        playerArray=players;
        if(playerArray.size()==2){
            dos1=playerArray.get(0).writer;
            dos2=playerArray.get(1).writer;
        }

    }





    //发牌给房间中的三位用户
    public static void distributePoker(Player player) throws IOException {

        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (playerArray.size() == 2&&count==0) {
                    try {
                        count++;
                        sendInfo(player);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Date(), 1500);



    }
    public static void jieguo(int playIndex) throws IOException {
        dos1.write(playIndex+"$");
        System.out.println(playIndex+"#"+"ok"+"$");
        dos1.newLine();
        dos1.flush();
        dos2.write(playIndex+"#"+"ok"+"$");
        System.out.println(playIndex+"$");
        dos2.newLine();
        dos2.flush();
        playerArray.clear();
    }
    public static void sendPai(int playIndex, String s) throws IOException {

        if(s.equals("要")){
            System.out.println("score1"+score1);
            System.out.println("score2"+score2);

                if (playIndex == 1) {
                    score1 += getValue(list.get(o));
                    if(score1>21){
                        System.out.println(playIndex+"#"+"end");
                        dos1.write(playIndex+"#"+"end");
                        dos1.newLine();
                        dos1.flush();
                        dos2.write(playIndex+"#"+"end");
                        dos2.newLine();
                        dos2.flush();
                        Server.removeAll();
                        playerArray.clear();
                        MyThread.users=new User[3];
                    }else {
                        System.out.println("score1" + score1);
                        System.out.println("score2" + score2);
                        dos1.write(playIndex + "#" + list.get(o) + "$");
                        System.out.println(playIndex + "#" + list.get(o) + "$");
                        dos1.newLine();
                        dos1.flush();
                        dos2.write(playIndex + "#" + list.get(o) + "$");
                        System.out.println(playIndex + "#" + list.get(o) + "$");
                        dos2.newLine();
                        dos2.flush();
                        o++;
                    }
                } else {
                    score2 += getValue(list.get(o));
                    if(score2>21){
                        System.out.println(playIndex+"#"+"end");
                        dos1.write(playIndex+"#"+"end");
                        dos1.newLine();
                        dos1.flush();
                        dos2.write(playIndex+"#"+"end");
                        dos2.newLine();
                        dos2.flush();
                        Server.removeAll();
                        playerArray.clear();
                        MyThread.users=new User[3];
                    }else {
                        System.out.println("score1" + score1);
                        System.out.println("score2" + score2);
                        dos1.write(playIndex + "#" + list.get(o) + "$");
                        System.out.println(playIndex + "#" + list.get(o) + "$");
                        dos1.newLine();
                        dos1.flush();
                        dos2.write(playIndex + "#" + list.get(o) + "$");
                        System.out.println(playIndex + "#" + list.get(o) + "$");
                        dos2.newLine();
                        dos2.flush();
                        o++;
                    }
                }

        }else{
            buyao++;
            System.out.println(buyao);
            if(buyao==1){
                System.out.println(playIndex+"#"+"jump");
                dos1.write(playIndex+"#"+"jump");
                dos1.newLine();
                dos1.flush();
                dos2.write(playIndex+"#"+"jump");
                dos2.newLine();
                dos2.flush();
            }else if(buyao==2){
                if(score1>score2){
                    System.out.println(2+"#"+"end");
                    dos1.write(2+"#"+"end");
                    dos1.newLine();
                    dos1.flush();
                    dos2.write(2+"#"+"end");
                    dos2.newLine();
                    dos2.flush();
                }else if(score1<score2){
                    System.out.println(1+"#"+"end");
                    dos1.write(1+"#"+"end");
                    dos1.newLine();
                    dos1.flush();
                    dos2.write(1+"#"+"end");
                    dos2.newLine();
                    dos2.flush();
                }else{
                    System.out.println(3+"#"+"end");
                    dos1.write(3+"#"+"end");
                    dos1.newLine();
                    dos1.flush();
                    dos2.write(3+"#"+"end");
                    dos2.newLine();
                    dos2.flush();
                }
                Server.removeAll();
                playerArray.clear();
                MyThread.users=new User[3];
            }
        }

    }
    private static void sendInfo(Player player) throws IOException {
        dos1.write(playerArray.get(0).getUserName()+"#"+playerArray.get(1).getUserName());
        dos1.newLine();
        dos1.flush();
        dos2.write(playerArray.get(0).getUserName()+"#"+playerArray.get(1).getUserName());
        dos2.newLine();
        dos2.flush();
//        //洗牌
        Collections.shuffle(list);
        //发牌
        //遍历牌库得到每一张牌
        for (int i = 0; i < 4; i++) {
            o++;
            //i为索引
            String poker = list.get(i);
            //给三个玩家轮流发牌
            if (i % 2 == 0) {
                player1Card.add(poker);
                score1+=getValue(list.get(i));
            } else {
                player2Card.add(poker);
                score2+=getValue(list.get(i));
            }
        }
        //给每个玩家的牌进行排序
        //order(lord);
        order(player1Card);
        order(player2Card);

        //distribute#[card]$
        //startGame#索引#牌$
        String p1Poker="startGame#"+"1#"+player1Card.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ") + "$";
        String p2Poker="startGame#"+"2#"+player2Card.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ") + "$";

        dos1.write(p1Poker);
        dos1.newLine();
        dos1.flush();
        dos1.newLine();
        dos1.flush();

        dos2.write(p2Poker);
        dos2.newLine();
        dos2.flush();
        dos2.newLine();
        dos2.flush();
    }


    //给牌排序
    //利用牌的价值进行排序
    //参数：集合
    public static void order(ArrayList<String> list) {
        Collections.sort(list, new Comparator<String>() {
            //Array.sort(插入排序+二分查找）
            @Override
            public int compare(String o1, String o2) {
                //o1:表示当前要插入到有序序列的牌
                //o2:表示已经在有序序列中存在的牌

                //负数：o1小 插入到后面
                //正数：o1大 插入到后面
                //0：o1的数字跟o2的数字是一样的需要按花色再次排序

                //1.计算o1的花色和价值
                String color1 = o1.substring(0, 1);
                int value1 = getValue(o1);

                //2.计算o2的花色和价值
                String color2 = o2.substring(0, 1);
                int value2 = getValue(o2);

                //3.比较o1和o2的价值
                int i = value2 - value1;

                return i == 0 ? color1.compareTo(color2) : i;
            }
        });

    }
    public static void sendToOthers1(int playerIndex,String message){
        try {
            System.out.println(playerIndex%3);
            playerArray.get(playerIndex%3).writer.write(message);
            playerArray.get(playerIndex%3).writer.newLine();
            playerArray.get(playerIndex%3).writer.flush();
            playerArray.get((playerIndex+1)%3).writer.write(message);
            playerArray.get((playerIndex+1)%3).writer.newLine();
            playerArray.get((playerIndex+1)%3).writer.flush();
            System.out.println("发送"+message);
        } catch (IOException e) {
            System.out.println("向另外两人发消息异常");
            throw new RuntimeException(e);
        }
    }
    //计算牌的价值
    //参数：牌
    //返回值：价值
    public static int getValue(String poker) {
        String number = poker.substring(1);
        //拿着数字到map集合中判断是否存在
        //存在，获取价值
        //不存在，类型转换
        if (hm.containsKey(number)) {
            return hm.get(number);
        } else {
            return Integer.parseInt(number);
        }

    }


}

